﻿using Blm.Utils.Extensions;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using VisionCloud.Core.Model.Enums;
using VisionCloud.Domain.DtoModel.Ms;
using VisionCloud.Utility;
using VisionCloud.Utility.Helpers;

namespace VisionCloud.Web
{
    public class UnzipTaskDataDBHelper
    {
        public static void UpdateFileStatusToNeedHandle(List<UnzipTaskDataEntity> unzipTaskDataEntityList, PostgreHelper postgreHelper)
        {
            UpdateFileStatus(unzipTaskDataEntityList, FileStatusEnum.待处理, postgreHelper);
        }

        public static void UpdateFileStatus(List<UnzipTaskDataEntity> unzipTaskDataEntityList, FileStatusEnum fileStatus, PostgreHelper postgreHelper)
        {
            try
            {
                using var dataSet = postgreHelper.ReadDataSet(@"select * from ""MsFileStatus"" where 1=0;");
                if (dataSet == null)
                    return;

                DataTable dt = dataSet.Tables[0];
                foreach (var unzipTaskDataEntity in unzipTaskDataEntityList)
                {
                    if (!unzipTaskDataEntity.IsHaveError)
                    {
                        try
                        {
                            DataRow dr = dt.NewRow();
                            dr["FileKey"] = unzipTaskDataEntity.FileKey;
                            dr["FileStatus"] = fileStatus.ToString();
                            dr["IsDeleted"] = false;
                            dr["LastUpdatedBy"] = "UnzipTaskDataService";
                            dr["CreatedDate"] = DateTime.Now;
                            dr["LastUpdatedDate"] = DateTime.Now;
                            dr["UsbKeyNO"] = unzipTaskDataEntity.UsbKeyNO;
                            dr["SyncStatus"] = "10";
                            dt.Rows.Add(dr);
                            postgreHelper.AddDataSetToDbTable("MsFileStatus", $"\"FileKey\"='{unzipTaskDataEntity.FileKey}'", dt, new DataColumn[] { dt.Columns["FileKey"] });
                            dt.Clear();
                            Logger.Info(string.Format("UnzipTaskDataDBHelper的参数UpdateFileStatus为1:{0}", unzipTaskDataEntity.FileKey));
                        }
                        catch (Exception ex)
                        {
                            unzipTaskDataEntity.IsHaveError = true;
                            DataRow dr = dt.NewRow();
                            dr["FileKey"] = unzipTaskDataEntity.FileKey;
                            dr["FileStatus"] = FileStatusEnum.处理失败.ToString();
                            dr["IsDeleted"] = false;
                            dr["LastUpdatedBy"] = "UnzipTaskDataService";
                            dr["CreatedDate"] = DateTime.Now;
                            dr["LastUpdatedDate"] = DateTime.Now;
                            dr["UsbKeyNO"] = unzipTaskDataEntity.UsbKeyNO;
                            dr["SyncStatus"] = "10";
                            dt.Rows.Add(dr);
                            postgreHelper.AddDataSetToDbTable("MsFileStatus", $"\"FileKey\"='{unzipTaskDataEntity.FileKey}'", dt, new DataColumn[] { dt.Columns["FileKey"] });
                            dt.Clear();
                            Logger.Error(ex.Message, ex);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Logger.Error(e.Message, e);
                Logger.Info(string.Format("UnzipTaskDataDBHelper的参数UpdateFileStatus为2:{0}", unzipTaskDataEntityList.Select(x=>new { x.FileKey}).ToJsonStr()));
            }
        }


        public static void UpdateFileStatus(List<FileInfo> listFile, FileStatusEnum fileStatus, UnzipTaskDataEntity unzipTaskDataEntity, PostgreHelper postgreHelper)
        {
            try
            {
                using var dataSet = postgreHelper.ReadDataSet(@"select * from ""MsFileStatus"" where 1=0;");
                if (dataSet == null)
                    return;

                DataTable dt = dataSet.Tables[0];
                foreach (var fileInfo in listFile)
                {
                    DataRow dr = dt.NewRow();
                    dr["FileKey"] = unzipTaskDataEntity.FileKey;
                    dr["FileStatus"] = fileStatus.ToString();
                    dr["IsDeleted"] = false;
                    dr["LastUpdatedBy"] = "UnzipTaskDataService";
                    dr["CreatedDate"] = DateTime.Now;
                    dr["LastUpdatedDate"] = DateTime.Now;
                    dr["UsbKeyNO"] = unzipTaskDataEntity.UsbKeyNO;// fileInfo.Name.Split('-')[0];
                    dr["SyncStatus"] = "10";
                    dt.Rows.Add(dr);
                    postgreHelper.AddDataSetToDbTable("MsFileStatus", $"\"FileKey\"='{unzipTaskDataEntity.FileKey}'", dt, new DataColumn[] { dt.Columns["FileKey"] });
                    dt.Clear();
                }
            }
            catch (Exception e)
            {
                Logger.Error(e.Message, e);
            }
        }
    }
}